<template>
  <div>
    <el-drawer size="80%"  v-model="open" :title="$t('恢复备份')" @close="closeDialog">
      <el-card class="box-card">
        <el-form label-width="500px" label-position="left">
          <el-form-item :label="$t('仓库名')">
            <el-input v-model="repository" readonly />
          </el-form-item>
          <el-form-item :label="$t('快照名')">
            <el-input v-model="snapshot" readonly />
          </el-form-item>
          <el-form-item :label="$t('需要恢复的索引')">
            <index-select
              :multiple="true"
              :have-all="true"
              :clearable="true"
              style="width: 210px"
              :placeholder="$t('需要恢复的索引')"
              @change="changeIndex"
            />
          </el-form-item>
          <el-form-item :label="$t('rename_pattern 【正则表达式】')">
            <el-input v-model="form.rename_pattern" />
          </el-form-item>
          <el-form-item :label="$t('rename_replacement 【正则表达式】')">
            <el-input v-model="form.rename_replacement" />
          </el-form-item>

          <!--          <el-form-item
            :label="$t('ignore_unavailable   【把这个选项设置为 true 的时候在创建快照的过程中会忽略不存在的索引,如果没有设置ignore_unavailable，在索引不存在的情况下快照请求将会失败。】')"
          >
            <el-select v-model="form.ignore_unavailable" filterable>
              <el-option :label="$t('不设置')" :value="null" />
              <el-option :label="$t('是')" :value="true" />
              <el-option :label="$t('否')" :value="false" />
            </el-select>
          </el-form-item>-->
          <el-form-item
            :label="$t('include_global_state 【通过设置 include_global_state 为false 能够防止 集群的全局状态被作为快照的一部分存储起来】')"
          >
            <el-select v-model="form.include_global_state" filterable>
              <el-option :label="$t('不设置')" :value="null" />
              <el-option :label="$t('是')" :value="true" />
              <el-option :label="$t('否')" :value="false" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('partial  【如果快照中的1个或多个索引不是全部主分片都可用，就会导致整个创建快照的过程失败。 通过设置 partial 为 true 可以改变这个行为】')">
            <el-select v-model="form.partial" clearable filterable>
              <el-option :label="$t('不设置')" :value="null" />
              <el-option :label="$t('是')" :value="true" />
              <el-option :label="$t('否')" :value="false" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('创建方式')">
            <el-select v-model="form.wait" clearable filterable>
              <el-option :label="$t('不设置')" :value="null" />
              <el-option :label="$t('异步创建')" :value="true" />
              <el-option :label="$t('同步创建')" :value="false" />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button

            type="danger"

            @click="closeDialog"
          >{{ $t('取消') }}
          </el-button>
          <el-button

            type="primary"

            @click="confirm"
          >{{ $t('确认') }}
          </el-button>
        </template>
      </el-card>
    </el-drawer>
  </div>
</template>


<script setup>
import {getCurrentInstance, ref, watch} from 'vue'
import { SnapshotRestoreAction } from '@/api/es-backup'
import { Close } from '@/api/es-index'
import { ElMessage, ElMessageBox } from 'element-plus'
const ctx = getCurrentInstance().appContext.config.globalProperties
import {sdk} from "@elasticview/plugin-sdk"
import IndexSelect from "@/components/index/select.vue";
// Props
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  snapshot: {
    type: String,
    default: ''
  },
  repository: {
    type: String,
    default: ''
  }
})
const snapshot = ref(props.snapshot)
const repository = ref(props.repository)
const open = ref(props.open)
// Local state
const isOpen = ref(false)
const form = ref({
  snapshotName: props.snapshot,
  repositoryName: props.repository,
  ignore_unavailable: null,
  include_global_state: null,
  partial: null,
  wait: null,
  indexList: [],
  rename_pattern: '',
  rename_replacement: ''
})

// Methods
const changeIndex = (index) => {
  form.value.indexList = index
}

const closeDialog = () => {
  emit('close', false)
}

const confirm = async () => {
  const input = { ...form.value, es_connect: sdk.GetSelectEsConnID() }
  const { code, msg } = await SnapshotRestoreAction(input)

  if (code === 0) {
    emit('close', true)
    ElMessage({
      type: 'success',
      message: msg
    })
    return
  }

  if (msg.includes(' with same name already exists ')) {
    ElMessageBox.confirm(`错误信息:【${msg}】,是否先关闭所有索引再进行恢复快照?`, '警告', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    })
        .then(async () => {
          const closeInput = {
            es_connect: sdk.GetSelectEsConnID(),
            index_name: '*'
          }
          const res = await Close(closeInput)
          if (res.code !== 0) {
            ElMessage({
              type: 'error',
              message: res.msg
            })
            return
          }
          await confirm()
        })
        .catch((err) => {
          console.error(err)
        })
  } else {
    ElMessage({
      type: 'error',
      message: msg
    })
  }
}

const emit = defineEmits(['close'])

// Watchers
watch(() => props.snapshot, (newVal) => {
  form.value.snapshotName = newVal
})

watch(() => props.repository, (newVal) => {
  form.value.repositoryName = newVal
})
</script>


<style scoped>

</style>
